<!--
 * @Description: 设备列表
 * @Author: charles
 * @Date: 2021-12-14 22:07:02
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-12-22 11:46:31
-->
<template>
  <!-- template模板中用于编写html代码 -->
  <div>
    <!-- 按钮区域 -->
    <div>
      <!-- 新增按钮 -->
      <el-button @click="openHandler" type="primary" size="small"
        >添加</el-button
      >
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="tableData.list" style="width: 100%" size="small">
        <el-table-column label="序号" type="index" width="50" align="center">
        </el-table-column>
        <el-table-column prop="name" label="设备名称"> </el-table-column>
        <el-table-column prop="serial_number" label="设备编号" width="80">
        </el-table-column>
        <el-table-column
          prop="longitude"
          label="经度"
          width="160"
          align="center"
        >
          <template v-slot="scope">
            <el-tag
              v-if="scope.row.longitude == null || scope.row.longitude == ''"
              size="mini"
              type="info"
              >暂无</el-tag
            >
            <span v-else>{{ scope.row.longitude }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="latitude"
          label="纬度"
          width="160"
          align="center"
        >
          <template v-slot="scope">
            <el-tag
              v-if="scope.row.latitude == null || scope.row.latitude == ''"
              size="mini"
              type="info"
              >暂无</el-tag
            >
            <span v-else>{{ scope.row.latitude }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="video" label="视频" align="center">
          <template v-slot="scope">
            <el-tag
              v-if="scope.row.video == null || scope.row.video == ''"
              size="mini"
              type="info"
              >暂无</el-tag
            >
            <span v-else>{{ scope.row.video }}</span>
          </template>
        </el-table-column>
        <el-table-column label="绑定状态" width="80" align="center">
          <template v-slot="scope">
            <el-tag v-if="scope.row.bind_status == 1" size="mini"
              >已绑定</el-tag
            >
            <el-tag v-else type="danger" size="mini">未绑定</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="在线状态" width="80" align="center">
          <template v-slot="scope">
            <el-tag v-if="scope.row.online_status == 1" size="mini"
              >在线</el-tag
            >
            <el-tag v-else type="danger" size="mini">离线</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="80" align="center">
          <template v-slot="scope">
            <el-button type="text" @click="toEditHandler(scope.row)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 模态框区域 -->
    <el-dialog
      @close="dialogCloseHandler"
      width="40%"
      :title="title"
      :visible.sync="visible"
    >
      <el-form ref="deviceForm" :model="form" :rules="rules">
        <el-form-item
          prop="serial_number"
          label="设备编号"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.serial_number"></el-input>
        </el-form-item>
        <el-form-item
          prop="name"
          label="设备名称"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="commitHandler">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分页组件 -->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="parseInt(tableData.page)"
      :page-size="parseInt(tableData.pageSize)"
      layout="total, sizes, prev, pager, next"
      :total="tableData.total"
      :page-sizes="[5, 10, 15, 20]"
    >
    </el-pagination>
  </div>
</template>
<script>
import { get, postJSON } from "@/utils/request";
export default {
  data() {
    return {
      name: "zhangsan",
      tableData: {},
      visible: false,
      form: {},
      formLabelWidth: "80px",
      title: "",
      params: {
        page: 1,
        pageSize: 10,
      },
      rules: {
        serial_number: [
          { required: true, message: "请输入设备编号", trigger: "blur" },
        ],
        name: [{ required: true, message: "请输入设备名称", trigger: "blur" }],
      },
    };
  },
  methods: {
    // pageSize改变处理函数
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      // 重载数据
      this.pageQueryDevices();
    },
    // page改变处理函数
    handleCurrentChange(page) {
      this.params.page = page;
      // 重载数据
      this.pageQueryDevices();
    },
    // 分页查询设备数据
    pageQueryDevices() {
      // 使用axios封装的get方法，发送http请求
      get("/device/pageQuery", this.params).then((res) => {
        // 将后台接口返回的结果，赋值给tableData
        this.tableData = res.data;
      });
    },
    // 添加按钮点击事件
    openHandler() {
      // 置空表单
      this.form = {};
      // 先设置模态框的标题
      this.title = "添加设备";
      // 最后打开模态框
      this.visible = true;
    },
    // 表单确定按钮点击事件
    commitHandler() {
      // 表单提交之前做表单验证
      this.$refs["deviceForm"].validate((valid) => {
        // 表单验证通过后，再去请求后台接口
        if (valid) {
          postJSON("/device/saveOrUpdate", this.form).then((res) => {
            // 操作成功之后的业务处理
            // 刷新数据
            this.pageQueryDevices();
            // 提示成功信息
            this.$message({
              message: "操作成功",
              type: "success",
            });
            // 关闭模态框
            this.visible = false;
          });
        } else {
          // 表单验证失败直接返回false
          return false;
        }
      });
    },
    // 编辑按钮点击事件
    toEditHandler(row) {
      // 将当前行数据中，设备的名称以及编号，设置到表单中
      this.form = {
        id: row.id,
        name: row.name,
        serial_number: row.serial_number,
      };
      // 修改模态框标题
      this.title = "修改设备";
      // 打开模态框
      this.visible = true;
    },
    // 模态框关闭回调
    dialogCloseHandler() {
      // 模态框关闭之后，清空表单验证
      this.$refs["deviceForm"].resetFields();
    },
  },
  created() {
    // 在vue的created生命周期阶段，调用查询设备的方法
    this.pageQueryDevices();
  },
};
</script>
<style scoped>
/* scoped标识当前样式表是有局部作用于的，为了防止样式的全局污染 */
</style>